/*
* YY图传配套协议
*/
#include "zf_common_headfile.h"
//需要自行修改
#define yy_send_buffer(dat,len); uart_write_buffer(UART_8, dat, len);

uint16 swj_CRC(uint8* Buf, uint8 CRC_sta, uint8 CRC_CNT)
{
    uint16 CRC_Temp;
    uint8 i, j;
    CRC_Temp = 0xffff;

    for (i = CRC_sta;i < CRC_CNT; i++) {
        CRC_Temp ^= Buf[i];
        for (j = 0;j < 8;j++) {
            if (CRC_Temp & 0x01)
                CRC_Temp = (CRC_Temp >> 1) ^ 0xa001;
            else
                CRC_Temp = CRC_Temp >> 1;
        }
    }
    return(CRC_Temp);
}

//带有校验的数据示波
void put_int32(uint8 name, int dat)
{
    uint8 datc[14] = { 102,102,198,1, name,0,0,0,0,0,0,198,102,102 };
    datc[5] = (uint8)(dat & 0xFF);
    datc[6] = (uint8)((dat & 0xFF00) >> 8);
    datc[7] = (uint8)((dat & 0xFF0000) >> 16);
    datc[8] = (uint8)((dat >> 24) & 0xFF);
    uint8 crc[6] = { 1, name,datc[5],datc[6],datc[7],datc[8] };
    uint16 CRC16 = swj_CRC(crc, 0, 6);
    datc[9] = (uint8)(CRC16 & 0xff);
    datc[10] = (uint8)(CRC16 >> 8);
    yy_send_buffer(datc, 14);
}

void put_float(uint8 name, float dat)
{
    uint8 datc[14] = { 102,102,198,2, name,0,0,0,0,0,0,198,102,102 };
    char farray[4] = { 0 };

    *(float*)farray = dat;
    unsigned char* p = (unsigned char*)&dat + 3;
    datc[5] = *(p - 3);
    datc[6] = *(p - 2);
    datc[7] = *(p - 1);
    datc[8] = *p;

    uint8 crc[6] = { 2, name,datc[5],datc[6],datc[7],datc[8] };
    uint16 CRC16 = swj_CRC(crc, 0, 6);
    datc[9] = (uint8)(CRC16 & 0xff);
    datc[10] = (uint8)(CRC16 >> 8);
    yy_send_buffer(datc, 14);
}

void myprintf(uint8* txt, uint8 len)
{
    uint8 head[5] = { 102,102,198,3,len };
    uint8 tail[3] = { 198,102,102};

    yy_send_buffer(head, 5);
    yy_send_buffer(txt, len);
    yy_send_buffer(tail, 3);
}
void sendpoint_clear(void)
{
    uint8 head[4] = { 102,102,198,4 };
    uint8 tail[3] = { 198,102,102 };

    yy_send_buffer(head, 4);
    yy_send_buffer(tail, 3);
}

//0:红 1：绿 2：蓝 3：青 4：紫 5：橙 6：黄 7：粉
void sendpoint_xy(uint8* x, uint8* y, uint8 len,uint8 color)
{
    uint8 head[6] = { 102,102,198,5,len,color};
    uint8 tail[3] = { 198,102,102 };

    yy_send_buffer(head, 6);
    yy_send_buffer(x, len);
    yy_send_buffer(y, len);
    yy_send_buffer(tail, 3);

}

//0:红 1：绿 2：蓝 3：青 4：紫 5：橙 6：黄 7：粉
void sendpoint_line(uint8* x, uint8 len, uint8 color)
{
    uint8 head[6] = { 102,102,198,6,len,color };
    uint8 tail[3] = { 198,102,102 };

    yy_send_buffer(head, 6);
    yy_send_buffer(x, len);
    yy_send_buffer(tail, 3);
}
